/**
 * Created by tangzk on 2016/7/2.
 */

;(function ($, win) {
    var boNiu = {},
        isTop = top.location == location ? true : false;
    boNiu.Util = {
        // 将元素上的参数字符串解析成数组
        parseElmOption: function ($elm) {
            var optStr = $elm.data('option'),
                optDict = {};
            if (optStr) {
                var optList = optStr.split(';');
                $.each(optList, function (k, optItem) {
                    if (optItem.indexOf(':') > 0) {
                        var optItemArr = optItem.split(':');
                        optDict[optItemArr[0]] = optItemArr[1];
                    }
                });
            }
            return optDict;
        }
    };

    boNiu.Dialog = {
        confirm: function (uOptions, noModal) {
            var dOptions = {
                title: '提示信息',
                width: 360,
                height: 120,
                okValue: '确认',
                cancelValue: '取消',
                quickClose: false,
                ok: function () {
                },
                cancel: function () {
                }
            };
            var options = $.extend(dOptions, uOptions);
            var d = dialog(options);
            noModal ? d.show() : d.showModal();
            return d;
        },
        page: function (uOptions, noModal) {
            var me,
                d,
                dOptions = {
                    title: false,
                    width: 600,
                    height: 400,
                    quickClose: false,
                    skin: 'frame-dialog',
                    oniframeload: function () {
                        var pageWin = d.iframeNode.contentWindow;
                        if (pageWin && pageWin.jQuery) {
                            var page$ = pageWin.jQuery;
                            page$('.__JpDialog').on('click', function () {
                                var $this = page$(this),
                                    action = $this.data('action');
                                if (action && uOptions[action]) {
                                    uOptions[action].call(me, pageWin);
                                }
                                else {
                                    d.close();
                                }
                            });
                        }
                    }
                },
                options = $.extend(dOptions, uOptions);
            d = dialog(options);
            noModal ? d.show() : d.showModal();
            return d;
        }
    };

    // 如页面为顶层窗口，则使用页面自己的Dialog插件，
    // 否则使用顶层窗口Dialog
    if (!isTop) {
        boNiu = top.boNiu;
    }


    win.boNiu = boNiu;
})(jQuery, window);
